<template>
	<view class="page">
		<view v-for="house in houses" :key="house.id">
			<uni-section :title="house.houseName" type="line">
				<uni-icons class="section-icon" type="more-filled" @click="clickHouse(house.houseName,house.id)"></uni-icons>
			</uni-section>
			<u-cell-group>
				<u-cell-item v-for="cote in house.cotes" :key="cote.id" icon="home-fill" :title="cote.coteName" @click="clickCote(cote.coteName,cote.id)"></u-cell-item>
			</u-cell-group>
		</view>
		<view class="house-footer">
		</view>
		<uni-fab :pattern="pattern" :content="content" horizontal="right" vertical="bottom" direction="vertical" @trigger="triggerFab"></uni-fab>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isFirstLoad: true,
				houses: [],
				pattern: {
					color: '#7A7E83',
					backgroundColor: '#fff',
					selectedColor: '#7A7E83',
					buttonColor: '#05ac9d'
				},
				content: [{
					iconPath: '/static/image/index-o.png',
					selectedIconPath: '/static/image/index-o.png',
					text: '畜舍',
					active: false
				}, {
					iconPath: '/static/image/house-o.png',
					selectedIconPath: '/static/image/house-o.png',
					text: '畜栏',
					active: false
				}]
			}
		},
		onLoad() {
			this.loadData()
		},
		onShow() {
			if (this.isFirstLoad) {
				this.isFirstLoad = false
			} else {
				this.loadData()
			}
		},
		methods: {
			loadData() {
				this.$u.api.queryCoteTree().then(res => {
					this.houses = res.data;
				})
			},
			clickHouse: function(name, id) {
				uni.showActionSheet({
					title: '操作',
					itemList: ['修改畜舍-' + name, '删除畜舍-' + name],
					success: (e) => {
						if (e.tapIndex === 0) {
							this.toHouseEditPage(id)
						} else {
							this.deleteHouse(id)
						}
					}
				})
			},
			clickCote: function(name, id) {
				uni.showActionSheet({
					title: '操作',
					itemList: ['修改畜栏-' + name, '删除畜栏-' + name],
					success: (e) => {
						if (e.tapIndex === 0) {
							this.toCoteEditPage(id)
						} else {
							this.deleteCote(id)
						}
					}
				})
			},
			toHouseAddPage: function() {
				uni.navigateTo({
					url: './house_add'
				});
			},
			toCoteAddPage: function() {
				uni.navigateTo({
					url: './cote_add'
				});
			},
			toHouseEditPage: function(id) {
				uni.navigateTo({
					url: './house_edit?id=' + id
				});
			},
			toCoteEditPage: function(id) {
				uni.navigateTo({
					url: './cote_edit?id=' + id
				});
			},
			deleteHouse: function(id) {
				const that = this
				uni.showModal({
					content: '确认删除该畜舍',
					success: function(res) {
						if (res.confirm) {
							uni.showLoading()
							that.$u.api.deleteHouse(id).then(res => {
								that.$dm.showSuccess()
								setTimeout(() => {
									that.loadData()
								}, 1000)
							}).catch(err => {
								uni.hideLoading()
							})
						}
					}
				});
			},
			deleteCote: function(id) {
				const that = this
				uni.showModal({
					content: '确认删除该畜栏',
					success: function(res) {
						if (res.confirm) {
							uni.showLoading()
							that.$u.api.deleteCote(id).then(res => {
								that.$dm.showSuccess()
								setTimeout(() => {
									that.loadData()
								}, 1000)
							}).catch(err => {
								uni.hideLoading()
							})
						}
					}
				});
			},
			triggerFab(e) {
				if (e.index === 0) {
					this.toHouseAddPage()
				} else {
					this.toCoteAddPage()
				}
			}
		}
	};
</script>

<style lang="scss" scoped>
	.house-title {
		height: 40px;
		line-height: 40px;
		width: 100%;
		background-color: #FFFFFF;
	}

	.section-icon {
		padding-right: 10rpx;
	}

	.uni-icons {
		line-height: 24px;
	}

	.house-footer {
		width: 100%;
		height: 150px;
	}
</style>
